
package darknet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.InetAddress;
import java.security.cert.Certificate;

/**
 * Represent informations:
 * 
 * - pseudo
 * - certificate
 * - inet address
 * - port
 *
 * @author yoyo
 */
public class PublicInformation implements Serializable {

    /**
     * Restaure a PublicInformation object serialised in a file.
     *
     * @param fileToPublicInformation The serialisation of a PublicInformation.
     * @return
     * @throws IOException
     */
    public static PublicInformation restaure(File fileToPublicInformation)
            throws IOException {

        if (fileToPublicInformation == null) {
            throw new IllegalArgumentException(
                    "fileToPublicInformation is null");
        }

        PublicInformation info = null;

        ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream(fileToPublicInformation));
        try {
            info = (PublicInformation) ois.readObject();
            ois.close();
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }

        return info;
    }

    /** Pseudo */
    private String pseudo;

    /** Certificate */
    private Certificate certificate;

    /** Inet Address */
    private InetAddress address;

    /** Port */
    private Integer port;

    /**
     * Initialise a new Public Information.
     *
     * @param pseudo
     * @param certificate
     * @param address
     * @param port
     */
    public PublicInformation(String pseudo, Certificate certificate,
            InetAddress address, Integer port) {

        if (pseudo == null || pseudo.length() == 0) {
            throw new IllegalArgumentException("pseudo is invalid");
        }
        if (certificate == null) {
            throw new IllegalArgumentException("certificate is null");
        }
        if (address == null) {
            throw new IllegalArgumentException("address is null");
        }
        if (port == null || port < 1 && port > 65536) {
            throw new IllegalArgumentException("port is invalid");
        }

        this.pseudo = pseudo;
        this.certificate = certificate;
        this.address = address;
        this.port = port;
    }

    /**
     * Serialise the object into a file.
     *
     * @param fileToPublicInformation The file to save the object.
     * @throws IOException
     */
    public void save(File fileToPublicInformation) throws IOException {

        if (fileToPublicInformation == null) {
            throw new IllegalArgumentException(
                    "fileToPublicInformation is null");
        }

        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream(fileToPublicInformation));
        oos.writeObject(this);
        oos.flush();
        oos.close();
    }

    public InetAddress getAddress() {
        return address;
    }

    public Certificate getCertificate() {
        return certificate;
    }

    public Integer getPort() {
        return port;
    }

    public String getPseudo() {
        return pseudo;
    }
}
